/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\MicrosoftAjax.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.debug.js" />
_spBodyOnLoadFunctionNames.push("BinaryJamCommonTermsSearchScriptLoaders");


function BinaryJamCommonTermsSearchScriptLoaders() {
    var prefix = _spPageContextInfo.siteServerRelativeUrl;

    if (prefix != "/") {
        prefix += "/";
    }

    var chainBase = $LAB;
    
    if (typeof (jQuery) == 'undefined') {
        chainBase = chainBase.script(prefix + "BinaryJamCommon_v4/jquery.min.js").wait(function () {
            if (!jQuery.ui) {
                chainBase
                .script(prefix + "BinaryJamCommon_v4/jquery-ui.js").wait(function () {

                    //When doc ready run the func)
                    $(function () {

                        //For Proper SharePoint
                        var searchBox = $("input[id$='InputKeywords']");
                        if (!searchBox.length) {
                            //For Foundation
                            searchBox = $("#idSearchString");
                        }

                        if (searchBox.length) {

                            $("head").append("<link>");
                            css = $("head").children(":last");
                            css.attr({
                                rel: "stylesheet",
                                type: "text/css",
                                href: prefix + "BinaryJamCommon_v4/jquery-ui.css"
                            });

                            $.ajax({
                                url: prefix + "_vti_bin/ListData.svc/BinaryjamCommonTerms",
                                type: "GET",
                                dataType: "json",
                                success: function (json) {
                                    //see http://api.jquery.com/jQuery.map/             
                                    var dataMap = jQuery.map(json.d.results, function (item) { return item.Title });
                                    searchBox.autocomplete({ source: dataMap });
                                }
                            }); //.ajax   
                        }
                    });

                });
            }
        });
    }
}



